package nl.biz_e.site.client;

import nl.biz_e.gwt.baselib.ui.client.panels.BizeFooter;
import nl.biz_e.site.client.MyButton.MyButtonListener;
import nl.biz_e.site.shared.models.Content;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Biz_e_Site implements EntryPoint, MyButtonListener {
    private Widget content;
    private MyButton btnWie;
    private MyButton btnWat;
    private MyButton btnWaar;
    private Widget footer;
    private ContentServiceAsync contentService;

    /**
     * {@inheritDoc}
     */
    @Override
    public void onModuleLoad() {
    	BizeFooter f = new BizeFooter();
        RootPanel.get().add(f);
        contentService = GWT.create(ContentService.class);
        content = this.createContentWidget();
        this.createButtonBar();
        footer = f;// RootPanel.get("footer");
        this.onClick(btnWie);
    }

    private void createButtonBar() {
        Grid grid = new Grid(1, 5);
        grid.setStylePrimaryName("button_bar");
        grid.getCellFormatter().setStylePrimaryName(0, 0, "table_spacer");
        grid.getCellFormatter().setStylePrimaryName(0, 1, "button");
        grid.getCellFormatter().setStylePrimaryName(0, 2, "button");
        grid.getCellFormatter().setStylePrimaryName(0, 3, "button");
        grid.getCellFormatter().setStylePrimaryName(0, 4, "table_spacer");

        btnWie = new MyButton("wie", this);
        grid.setWidget(0, 1, btnWie);
        btnWat = new MyButton("wat", this);
        grid.setWidget(0, 2, btnWat);
        btnWaar = new MyButton("waar", this);
        grid.setWidget(0, 3, btnWaar);

        RootPanel.get("header").add(grid);
    }

    private Widget createContentWidget() {
        Grid grid = new Grid(3, 5);
        grid.getCellFormatter().setStylePrimaryName(0, 0, "table_spacer");
        grid.getCellFormatter().setStylePrimaryName(0, 1, "content_corner_lt");
        grid.getCellFormatter().setStylePrimaryName(0, 2, "content_bevel_top");
        grid.getCellFormatter().setStylePrimaryName(0, 3, "content_corner_rt");
        grid.getCellFormatter().setStylePrimaryName(0, 4, "table_spacer");

        grid.getCellFormatter().setStylePrimaryName(1, 0, "table_spacer");
        grid.getCellFormatter().setStylePrimaryName(1, 1, "content_bevel_left");
        grid.getCellFormatter().setStylePrimaryName(1, 2, "content_area");
        grid.getCellFormatter()
                .setStylePrimaryName(1, 3, "content_bevel_right");
        grid.getCellFormatter().setStylePrimaryName(1, 4, "table_spacer");

        grid.getCellFormatter().setStylePrimaryName(2, 0, "table_spacer");
        grid.getCellFormatter().setStylePrimaryName(2, 1, "content_corner_lb");
        grid.getCellFormatter().setStylePrimaryName(2, 2,
                "content_bevel_bottom");
        grid.getCellFormatter().setStylePrimaryName(2, 3, "content_corner_rb");
        grid.getCellFormatter().setStylePrimaryName(2, 4, "table_spacer");

        RootPanel.get("content").add(grid);
        final ScrollPanel scroll = new ScrollPanel();
        grid.setWidget(1, 2, scroll);

        HTML html = new HTML();
        html.setStylePrimaryName("content_text");
        scroll.add(html);

        Window.addResizeHandler(new ResizeHandler() {

            @Override
            public void onResize(ResizeEvent event) {
                resizeContent();
                scroll.onResize();
            }
        });

        return html;
    }

    private void resizeContent() {
        content.setHeight("auto");
        int scrollHeight = content.getOffsetHeight();
        int newMaxHeight = (footer.getAbsoluteTop() - content.getAbsoluteTop()) - 50;
        if (newMaxHeight < 50)
            newMaxHeight = 50;
        if (scrollHeight > newMaxHeight) {
            content.setHeight(String.valueOf(newMaxHeight) + "px");
        }
    }

    private void showContent(String id) {
        contentService.getContent(id, new AsyncCallback<Content>() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void onSuccess(Content result) {
                setContent(result);

            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void onFailure(Throwable caught) {
                setContent("Sorry<br/>" + caught);
            }
        });
    }

    private void setContent(Content c) {
        this.setContent(c.getText());
    }

    private void setContent(String contentHtml) {
        content.getElement().setInnerHTML(contentHtml);
        resizeContent();
    }

    private void selectButton(MyButton button) {
        btnWie.setSelected(button == btnWie);
        btnWat.setSelected(button == btnWat);
        btnWaar.setSelected(button == btnWaar);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onClick(MyButton source) {
        selectButton(source);
        showContent(source.getId());
    }

}
